<?php
	set_time_limit(0);
	$host="127.0.0.1";
	$port=9526;
	$socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("cannot create socket\n");
	$result=socket_bind($socket,$host,$port) or die("canot bind port to socket\n");
	$result=socket_listen($socket,4) or die("could not set up socket listen\n");
	$msgsock=socket_accept($socket) or die("cannot accept incoming connection");
	if ($msgsock) {
	//	echo date("Y-m-d H:i:s D a")."This is hsq socket";
		echo "socket work out<br>";
	}
	$input= socket_read($msgsock, 1024) or die ("cannot read input\n");
	// woshou($msgsock,$input);
	// while (true) {
		
	// 		$output="hsqmsf\n";
	// 		socket_write($msgsock, $output,strlen($output)) or die("cannot write");
	
	// }
	$input = trim($input);
//	$output="this message from server by hsq\n";
	echo "message from client is<br>";
	echo $input;
	$output=$input."\n";	
	socket_write($msgsock, $output,strlen($output)) or die("cannot write");
	socket_close($msgsock);
	socket_close($socket);
	// function woshou ( $ws , $buffer )
 //    {
 //        $buf = substr ( $buffer , strpos ( $buffer , 'Sec-WebSocket-Key:' ) + 18 ) ;
 //        $key = trim ( substr ( $buf , 0 , strpos ( $buf , "\r\n" ) ) ) ;

 //        $new_key = base64_encode ( sha1 ( $key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ,
 //                        true ) ) ;

 //        $new_message = "HTTP/1.1 101 Switching Protocols\r\n" ;
 //        $new_message .= "Upgrade: websocket\r\n" ;
 //        $new_message .= "Sec-WebSocket-Version: 13\r\n" ;
 //        $new_message .= "Connection: Upgrade\r\n" ;
 //        $new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n" ;

 //        socket_write ( $ws , $new_message ,
 //                strlen ( $new_message ) ) ;
  
 //    }
?>